home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / INDEX.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-25  |  1.7 KB  |  65 lines

  1. ; INDEX- computes the offset of one string within another.
  2. ;
  3. ; On entry:
  4. ;
  5. ; ES:DI-            Points at the test string that INDEX will search for
  6. ;            in the source string.
  7. ; DS:SI-            Points at the source string which (presumably) 
  8. ;             contains the string INDEX is searching for.
  9. ;
  10. ; On exit:
  11. ;
  12. ; AX-            Contains the offset into the source string where the 
  13. ;             test string was found.
  14.  
  15. INDEX        proc    near
  16.         push    si
  17.         push    di
  18.         push    bx
  19.         push    cx
  20.         pushf            ;Save direction flag value.
  21.         cld 
  22.  
  23.         mov    al, es:[di]    ;Get the length of the test string.
  24.         cmp    al, [si]    ;See if it is longer than the length
  25.         ja    NotThere    ; of the source string.
  26.  
  27. ; Compute the index of the last character we need to compare the
  28. ; test string against in the source string.
  29.  
  30.         mov    al, es:[di]    ;Length of test string.
  31.         mov    cl, al        ;Save for later.
  32.         mov    ch, 0
  33.         sub    al, [si]    ;Length of source string.
  34.         mov    bl, al        ;# of times to repeat loop.
  35.         inc    di        ;Skip over length byte.
  36.         xor    ax, ax        ;Init index to zero.
  37. CmpLoop:    inc    ax        ;Bump index by one.
  38.         inc    si        ;Move on to the next char in source.
  39.         push    si        ;Save string pointers and the
  40.         push    di        ; length of the test string.
  41.         push    cx
  42.     rep    cmpsb            ;Compare the strings.
  43.         pop    cx        ;Restore string pointers
  44.         pop    di        ; and length.
  45.         pop    si
  46.         je    Foundindex    ;If we found the substring.
  47.         dec    bl
  48.         jnz    CmpLoop        ;Try next entry in source string.
  49.  
  50. ; If we fall down here, the test string doesnâ•’t appear inside the
  51. ; source string.
  52.  
  53. NotThere:    xor    ax, ax        ;Return INDEX = 0
  54.  
  55. ; If the substring was found in the loop above, remove the
  56. ; garbage left on the stack
  57.  
  58. FoundIndex:        popf
  59.         pop    cx
  60.         pop    bx
  61.         pop    di
  62.         pop    si
  63.         ret
  64. INDEX        endp
  65.